var GameRoom = function(){
	var seat,pid,pname,rid;
	
	var myturn= false;
	var roomstatus = "inactive";
	var cmd = {
		init:function(){
			this.dom = $('cmd');
			this.p1 = this.dom.getElementsByTagName('div')[0];
			this.p2 = this.dom.getElementsByTagName('div')[1];
			this.showReady(true);
			this.initButton();
			this.disable();
		},
		showReady:function(flag){
			if(flag){
				this.p1.style.display="block";
				this.p2.style.display="none";
			}else{
				this.p1.style.display="none";
				this.p2.style.display="block";
			}
		},
		initButton:function(){
			var that = this;
			$("cmd_start").onclick=function(){
				that.showReady(false);
				Board.reset();
				me.setStatus("Ready");$('msg').innerHTML='等待对方出棋';
				new Ajax().send("CommandAction",{"data":"rid="+rid+"&seat="+seat+"&cmd=start"});
			};
			$("cmd_recall").onclick=function(){
				alert("心情不好，暂时不开放此功能");
				//new Ajax().send("CommandAction",{"data":"rid="+rid+"&seat="+seat+"&cmd=recall"});
			};
			$("cmd_giveup").onclick=function(){
				new Ajax().send("CommandAction",{"data":"rid="+rid+"&seat="+seat+"&cmd=giveup"});
			};
			$("cmd_turn").style.display="none";
			$("cmd_turn").onclick=function(){
					myturn=false;
					cmd.disable();
					new Ajax().send("CommandAction",{"data":"rid="+rid+"&seat="+seat+"&cmd=turn"});
				
			};
		},
		disable:function(){
			$("cmd_recall").disabled=true;
			$("cmd_giveup").disabled=true;
			$("cmd_turn").disabled=true;
			if(roomstatus=='active')$('msg').innerHTML='轮到对方出棋';
			else $('msg').innerHTML='请准备';
		},
		enable:function(){
			$("cmd_recall").disabled=false;
			$("cmd_giveup").disabled=false;
			$("cmd_turn").disabled=true;
			if(roomstatus=='active')$('msg').innerHTML='轮到自己出棋';
			else $('msg').innerHTML='请准备';
		}
	}
	var me = {
		init:function(){
			this.dom = $('me');
			this.name = this.dom.getElementsByTagName('span')[0];
			this.status = this.dom.getElementsByTagName('span')[1];
		},
		setName:function(name){
			this.name.innerText = name;
		},
		setStatus:function(status){
			this.status.innerText = status;
		}
	};
	var other = {
		init:function(){
			this.dom = $('other');
			this.name = this.dom.getElementsByTagName('span')[0];
			this.status = this.dom.getElementsByTagName('span')[1];
			this.image = this.dom.getElementsByTagName('img')[0];
		},
		setName:function(name){
			this.name.innerText = name;
		},
		setStatus:function(status){
			this.status.innerText = status;
		},
		setImage:function(url){
			this.image.src = url;
		}
	};
	var Board = {
		lastSelected:null,
		chessmanHolder:{},
		init:function(seat){
			this.dom = $('qipan');
			this.map = this.dom.getElementsByTagName('table')[0];
			this.oldpath = document.createElement('img');
			this.newpath = document.createElement('img');
			this.oldpath.src = 'image/oldpath.png'
			this.newpath.src='image/newpath.png';
			this.oldpath.style.position = this.newpath.style.position='absolute';
			this.oldpath.style.display="none";
			this.newpath.style.display="none";
			Path.dom.appendChild(this.oldpath);
			Path.dom.appendChild(this.newpath);
			this.dom.onclick=function(){
				if(Board.lastSelected!=null)Board.lastSelected.blur();
			};
			var chesstype =['C','M','X','S','J','S','X','M','C','P','P','B','B','B','B','B'];
			this.mapArr = [
							['#','#','#','#','#','#','#','#','#'],
							['','','','','','','','',''],
							['','#','','','','','','#',''],
							['#','','#','','#','','#','','#'],
							['','','','','','','','',''],
							['','','','','','','','',''],
							['#','','#','','#','','#','','#'],
							['','#','','','','','','#',''],
							['','','','','','','','',''],
							['#','#','#','#','#','#','#','#','#'],
							];
							
			var me = seat;
			var other = seat=='A'?'B':'A';
			var count = 1;
			for(var i =0;i<4;i++){
				for(var j =0;j<9;j++){
					if(this.mapArr[i][j]=='#'){
						var t = chesstype.shift();
						var o = new ChessMan(count,i,j,other,t);
						var m = new ChessMan(count,9-i,8-j,me,t);
						this.add(o);
						this.add(m);
						count++;
					}
				}
			}
		},
		add:function(chessman){
			var i =chessman.i;
			var j = chessman.j;
		    var td =this.map.rows(i).cells(j);
		    td.appendChild(chessman.dom);
		    this.mapArr[i][j] = chessman.id;
		    this.chessmanHolder[chessman.id] = chessman;
		},
		move:function(chessman,i,j){
			var td =this.map.rows(i).cells(j);
			
			if(td.firstChild){
				td.removeChild(td.firstChild);
			}
		    td.appendChild(chessman.dom);
		    this.mapArr[chessman.i][chessman.j]='';
		    this.showPath(chessman.i,chessman.j,i,j);
			chessman.i = i;
			chessman.j = j;
		    this.mapArr[i][j] = chessman.id;
		},
		showPath:function(oi,oj,ni,nj){
			this.oldpath.style.display="block";
			this.newpath.style.display="block";
			this.oldpath.style.left = oj*70;
			this.oldpath.style.top = oi*70;
			this.newpath.style.left = nj*70;
			this.newpath.style.top = ni*70;
		},
		reset:function(){
			this.oldpath.style.display="none";
			this.newpath.style.display="none";
			for(var chessid in this.chessmanHolder){
				var chessman = this.chessmanHolder[chessid];
				  	var td =this.map.rows(chessman.oi).cells(chessman.oj);
		   			td.appendChild(chessman.dom);
		   			this.mapArr[chessman.i][chessman.j] ='';
		   			this.mapArr[chessman.oi][chessman.oj] = chessman.id;
		   			chessman.i = chessman.oi;
		   			chessman.j = chessman.oj;
					
			}
		}
	};
	
	var Path ={
		allIndicators:{},
		lastShow:[],
		init:function(){
			this.dom = $('holder');
			for(var i =0;i<10;i++){
				for(var j =0;j<9;j++){
					var indi = new Indicator(i,j);
					this.add(indi);
					this.allIndicators[""+i+j]=indi;
				}
			}
		},
		add:function(indicator){
		    this.dom.appendChild(indicator.dom);
		},
		hidePath:function(){
			for(var i =0;i<this.lastShow.length;i++){
				this.allIndicators[this.lastShow[i]].hide();
			}
			this.lastShow=[];
		},
		showPath:function(chessman){
			var type = chessman.type;
			var si = chessman.i;
			var sj = chessman.j;
			var belong = chessman.belong;
			switch(type){
				case "B":this.LogicB(si,sj,belong);break;
				case "C":this.LogicC(si,sj,belong);break;
				case "P":this.LogicP(si,sj,belong);break;
				case "M":this.LogicM(si,sj,belong);break;
				case "X":this.LogicX(si,sj,belong);break;
				case "S":this.LogicS(si,sj,belong);break;
				case "J":this.LogicJ(si,sj,belong);break;
			}
		},
		show:function(indexi,indexj,belong){
			var chessmanid = Board.mapArr[indexi][indexj];
				if(chessmanid!=''){
					var chessman = Board.chessmanHolder[chessmanid];
					if(chessman.belong==belong){
						return;
					}
				}
				var id = ""+indexi+indexj;
				this.allIndicators[id].show();
				this.lastShow.push(id);
		},
		LogicB:function(si,sj,belong){				
				if(0<si&&si<5){
					this.show(si-1,sj,belong);
					if(sj>0)this.show(si,sj-1,belong);
					if(sj<8)this.show(si,sj+1,belong);
				}else if(si>=5){
					this.show(si-1,sj,belong);
				}else{
					if(sj>0)this.show(si,sj-1,belong);
					if(sj<8)this.show(si,sj+1,belong);
				}
		},
		LogicC:function(si,sj,belong){
			//check left
			for(var k=sj-1;k>=0;k--){
				this.show(si,k,belong);
				if(Board.mapArr[si][k]!='')break;
			}	
			//check up
			for(var k=si-1;k>=0;k--){
				this.show(k,sj,belong);
				if(Board.mapArr[k][sj]!='')break;
			}
			//check right
			for(var k=sj+1;k<9;k++){
				this.show(si,k,belong);
				if(Board.mapArr[si][k]!='')break;
			}	
			//check down
			for(var k=si+1;k<10;k++){
				this.show(k,sj,belong);
				if(Board.mapArr[k][sj]!='')break;
			}		
		},
		LogicP:function(si,sj,belong){
			//check left
			for(var k=sj-1;k>=0;k--){
				if(Board.mapArr[si][k]!=''){
					for(var m=k-1;m>=0;m--){
						if(Board.mapArr[si][m]!=''){
							this.show(si,m,belong);
							break;
						}
					}
					break;
				}
				this.show(si,k,belong);
			}	
			//check up
			for(var k=si-1;k>=0;k--){
				if(Board.mapArr[k][sj]!=''){
					for(var m=k-1;m>=0;m--){
						if(Board.mapArr[m][sj]!=''){
							this.show(m,sj,belong);
							break;
						}
					}
					break;
				}
				this.show(k,sj,belong);
			}
			//check right
			for(var k=sj+1;k<9;k++){
				if(Board.mapArr[si][k]!=''){
					for(var m=k+1;m<9;m++){
						if(Board.mapArr[si][m]!=''){
							this.show(si,m,belong);
							break;
						}
					}
					break;
				}
				this.show(si,k,belong);
			}	
			//check down
			for(var k=si+1;k<10;k++){
				if(Board.mapArr[k][sj]!=''){
					for(var m=k+1;m<10;m++){
						if(Board.mapArr[m][sj]!=''){
							this.show(m,sj,belong);
							break;
						}
					}
					break;
				}
				this.show(k,sj,belong);
			
			}		
		},
		LogicM:function(si,sj,belong){
			//left
			if(sj>=2&&Board.mapArr[si][sj-1]==''){
				if(si>=1)this.show(si-1,sj-2,belong);
				if(si<=8)this.show(si+1,sj-2,belong);
			}
			//right
			if(sj<=7&&Board.mapArr[si][sj+1]==''){
				if(si>=1)this.show(si-1,sj+2,belong);
				if(si<=8)this.show(si+1,sj+2,belong);
			}
			//up
			if(si>=2&&Board.mapArr[si-1][sj]==''){
				if(sj>=1)this.show(si-2,sj-1,belong);
				if(sj<=7)this.show(si-2,sj+1,belong);
			}
			//down
			if(si<8&&Board.mapArr[si+1][sj]==''){
				if(sj>=1)this.show(si+2,sj-1,belong);
				if(sj<=7)this.show(si+2,sj+1,belong);
			}
		},
		LogicX:function(si,sj,belong){
			if(si==9){
				if(Board.mapArr[si-1][sj-1]=='')this.show(si-2,sj-2,belong);
				if(Board.mapArr[si-1][sj+1]=='')this.show(si-2,sj+2,belong);
			}else if(si==5){
				if(Board.mapArr[si+1][sj-1]=='')this.show(si+2,sj-2,belong);
				if(Board.mapArr[si+1][sj+1]=='')this.show(si+2,sj+2,belong);
			}else if(sj==0){
				if(Board.mapArr[si-1][sj+1]=='')this.show(si-2,sj+2,belong);
				if(Board.mapArr[si+1][sj+1]=='')this.show(si+2,sj+2,belong);
			}else if(sj==4){
				if(Board.mapArr[si-1][sj+1]=='')this.show(si-2,sj+2,belong);
				if(Board.mapArr[si+1][sj+1]=='')this.show(si+2,sj+2,belong);
				if(Board.mapArr[si-1][sj-1]=='')this.show(si-2,sj-2,belong);
				if(Board.mapArr[si+1][sj-1]=='')this.show(si+2,sj-2,belong);
			}else if(sj==8){
				if(Board.mapArr[si-1][sj-1]=='')this.show(si-2,sj-2,belong);
				if(Board.mapArr[si+1][sj-1]=='')this.show(si+2,sj-2,belong);
			}		
		},
		LogicS:function(si,sj,belong){
			if(si==9){
				if(sj==3)this.show(si-1,sj+1,belong);
				if(sj==5)this.show(si-1,sj-1,belong);
			}else if(si==7){
				if(sj==3)this.show(si+1,sj+1,belong);
				if(sj==5)this.show(si+1,sj-1,belong);
			}else if(si==8){
				this.show(si+1,sj+1,belong);
				this.show(si-1,sj-1,belong);
				this.show(si-1,sj+1,belong);
				this.show(si+1,sj-1,belong);
			}
		},
		LogicJ:function(si,sj,belong){
			//left
			if(sj>3){
				this.show(si,sj-1,belong);
			}
			if(sj<5){
				this.show(si,sj+1,belong);
			}
			if(si<9){
				this.show(si+1,sj,belong);
			}
			if(si>7){
				this.show(si-1,sj,belong);
			}
		}
	}
	function Indicator(i,j){
		this.i = i;
		this.j = j;
		this.dom = document.createElement('img');
		this.init();
	}
	Indicator.prototype={
		init:function(){
			this.dom.src="image/indicator.png";
			this.dom.style.position="absolute";
			this.dom.style.left=this.j*70;
			this.dom.style.top=this.i*70;
			this.dom.style.cursor="pointer";
			this.dom.style.zIndex="999";
			var that = this;
			this.dom.onclick=function(){
				if(myturn&&roomstatus=='active'){
					that.hide();
					var chessman = Board.lastSelected;
					var step = chessman.id+","+that.i+","+that.j;
					new Ajax().send("CommandAction",{"data":"rid="+rid+"&seat="+seat+"&cmd=step&step="+step});
					myturn=false;
					cmd.disable();
					var chessid = Board.mapArr[that.i][that.j];
					if(chessid!=''){
						if(Board.chessmanHolder[chessid].type=='J'){
							var belong = Board.chessmanHolder[chessid].belong;
							new Ajax().send("CommandAction",{"data":"rid="+rid+"&seat="+belong+"&cmd=giveup"});
						}
					}
					Board.move(chessman,that.i,that.j);
					Board.lastSelected.blur();
					event.cancelBubble=true;
				}
			}
			this.hide();
		},
		show:function(){
			this.dom.style.display="block";
		},
		hide:function(){
			this.dom.style.display="none";
		}
	}
	function ChessMan(id,i,j,belong,type){
		this.id = belong+id;
		this.oi = i;
		this.oj = j;
		this.i = i;
		this.j = j;
		this.belong = belong;
		this.type = type;
		this.dom = document.createElement('img');
		this.init();
	}
	ChessMan.prototype ={
		init:function(){
			this.dom.src='image/chessman/'+this.belong+this.type+'.png';
			this.dom.width=64;
			this.dom.height=64;
			if(seat==this.belong){
				var that =this;
				this.dom.style.cursor="pointer";
				this.dom.onclick=function(){
					if(myturn&&roomstatus=='active'){
						that.focus();
						event.cancelBubble=true;
					}
				}
			}
		},
		focus:function(){
			//if(Board.lastSelected==this)return;
			if(Board.lastSelected!=null)Board.lastSelected.blur();
			this.dom.width=70;
			this.dom.height=70;
			Board.lastSelected = this;
			Path.showPath(this);
		},
		blur:function(){
			this.dom.width=64;
			this.dom.height=64;
			Board.lastSelected = null;
			Path.hidePath();
		}
	};
	function startService(){
		var ajax = new Ajax();
			setInterval(function(){
				try{
				ajax.send("AjaxFetchRoomMsg",{"data":"rid="+rid+"&seat="+seat,"onsuccess":function(req){
					var xml = req.responseXML.documentElement;
					if(xml!=null){
						parseXML(xml.getElementsByTagName('message')[0]);
					}
				}});
				}catch(e){alert(e.message);}
			},100);
		}
		
	function parseXML(msg){
		var forwhich = msg.getAttribute('for');
		switch(forwhich){
			case "roomuser":roomuser(msg);break;
			case "readystate":readystate(msg);break;
		}
	}
	
	/*----------------------*/
	function roomuser(msg){
		var status = msg.getAttribute('status');
		var pname = msg.getAttribute('pname');
		var pid = msg.getAttribute('pid');
		
		if(pname!==''){
			other.setName(pname);
			other.setImage("image/portrait.png");
			if(status==='true')other.setStatus('Ready');
			else other.setStatus('Not Ready');
		}else{
			other.setName('N/A');
			other.setStatus('N/A');
			other.setImage("image/placeholder.png");
		}
	}
	function readystate(msg){
		var status = msg.getAttribute('status');
		if(status=='ready'){
			other.setStatus('Ready');
		}else if(status=='start'){
			var turn = msg.getAttribute('turn');
			myturn = (turn==seat);
			roomstatus = "active";
			if(myturn)cmd.enable();
			else{$('msg').innerHTML='轮到对方出棋';}
		}else if(status=='end'){
			var turn = msg.getAttribute('turn');
			if(turn==seat){
					alert("玩家认输");
			}else{
					alert("对方认输");
			}
			myturn=false;
			roomstatus="inactive";
			cmd.showReady(true);
			cmd.disable();
			me.setStatus("Not Ready");
			other.setStatus("Not Ready");
			
		}else if(status=='step'){
			var step = msg.getAttribute('turn');
			var arr = step.split(",");
			var chessid = arr[0];
			var toi = 9-arr[1];
			var toj = 8-arr[2];
			Board.move(Board.chessmanHolder[chessid],toi,toj);
		}
	}
	/*----------------------*/
	function init(){
		seat = window.dialogArguments.seat;
		pid = window.dialogArguments.pid;
		pname = window.dialogArguments.pname;
		rid = window.dialogArguments.rid;
		document.body.onunload=function(){
			new Ajax().send("ExitRoom",{"data":"rid="+rid+"&seat="+seat});
		};
		
		other.init();
		me.init();
		me.setName(pname);
		
		
		Path.init();
		Board.init(seat);
		cmd.init();
		
		startService();
	}
	return {"init":init};
}();